package Manager;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;

import javax.swing.*;

import CommonPanel.*;
import Database.*;
import Exception.*;
import General.*;
import GenericClass.*;
import GenericMethod.*;
import Manager.Account.*;
import Manager.Backup.*;
import Manager.Help.*;
import Manager.Student.*;
import Projo.*;
import Teacher.*;

public class WindowManager extends JFrame
{
	//Variable Declaration
	private CallDB callDB;
	private Connection connection;
	private SetDimension setDimension;
	
	private Container container;
	private JMenuBar menuBar;
	
	private JMenu menuHome;
	private JMenu menuNavigation;
	private JMenu menuAbout;
	
	private JMenuItem itemHome, itemSwitch, itemExit;
	private JMenuItem itemAccount, itemStudent, itemBackup;
	private JMenuItem itemFAQ, itemHelp, itemContact;
	
	private MainPanelManager mainPanelManager;
	private HeaderPanel headerPanel;
	private PanelAboutManager panelAboutManager;
	private PanelStudent panelStudent;
	private PanelBackup panelBackup;
	private PanelAccount panelAccount;
	private PanelCreateModAccount panelCreateModAccount;
	private JPanel currentPanel;

	private AccountMgmt accountList;
	private String titleLabel;
	
	private ArrayList<Student> arrayStudent = new ArrayList<Student>();
	private MyTableModel accountListModel;
	private MyActionListener actionListener;
	private FormatScrollPane formatScrollPane;
	
	private boolean panelAccountFlag = false, mainPanelManagerFlag = false, panelStudentFlag = false, panelAboutManagerFlag = false;
	private String pathElsa = "//localhost:3306/elsa", user = "root", password = "";
	
	public WindowManager() throws LoadDriverException, OpenConnectionException, CheckDataException
	{
		//Setting window
		super("Manager");
		
		setDimension = new SetDimension();
		setDimension.setBoundFrame(this, 800, 800);
		
		callDB = new CallDB(); //Initialize a CallDB object
		connection = callDB.openConnection(pathElsa, user, password);//Open a connection
		
		//GeneralMethod.setSession(session);
		
		//Listener
		MyWindowListener windowListener = new MyWindowListener(); //Listener of the window
		this.addWindowListener(windowListener); //Link the listener
		
		actionListener = new MyActionListener(); //Listener for the different panel
				
		addMenuBar(actionListener);
		addItemMenuListener(actionListener);
		
	    container = getContentPane();
		container.setLayout(new BorderLayout());
	    
		headerPanel = new HeaderPanel();
		
		showFirstPanelMain();
		
		formatScrollPane = new FormatScrollPane();
		
		currentPanel = new JPanel();
		currentPanel = mainPanelManager;
		
		container.add(headerPanel, BorderLayout.NORTH);
		container.add(mainPanelManager, BorderLayout.CENTER);

		setResizable(false);
		setVisible(true);
	}
	
	
	public void showFirstPanelMain() throws LoadDriverException, CheckDataException
	{		
		mainPanelManager = new MainPanelManager(callDB, connection);
		mainPanelManager.setName("mainPanelManager");
		
		mainPanelManager.getButtonAccount().addActionListener(actionListener);
		mainPanelManager.getButtonStudent().addActionListener(actionListener);
		mainPanelManager.getButtonBackup().addActionListener(actionListener);
	}
	
	public void showPanelMain() throws LoadDriverException, CheckDataException
	{
		mainPanelManager = new MainPanelManager(callDB, connection);
		mainPanelManager.setName("mainPanelManager");
		
		addNewAndRemoveOldPanel(mainPanelManager);
		
		mainPanelManager.getButtonAccount().addActionListener(actionListener);
		mainPanelManager.getButtonStudent().addActionListener(actionListener);
		mainPanelManager.getButtonBackup().addActionListener(actionListener);
	}
	
	public void showPanelAccount() throws CheckDataException, FormatScrollPaneException, LoadDriverException
	{
		setCurrentPanelFlags(false); //Change the current panel flag to false
		
		String[] tabHeader = {"Firstname", "Lastname"};
		GeneralMethod.clearAndSetHeader(tabHeader, "left");
		
		GeneralInformation.initAccountModel(connection);
		GeneralMethod.setLeftModel(GeneralInformation.getMyAccountModel()); //Set the Generic Left JTable by the MyTableModel
		
		String[] tabHeaderDescript = {"Course name"};
		GeneralMethod.clearAndSetHeader(tabHeaderDescript, "descript");
		
		accountList = new AccountMgmt(connection);
		accountListModel = accountList.selectAllElements();
		panelAccount = new PanelAccount(callDB, connection);
		panelAccount.setName("panelAccount");
		
		addNewAndRemoveOldPanel(panelAccount); //Change the current panel
		
		MyActionListener actionListener = new MyActionListener();
		panelAccount.getButtonCreate().addActionListener(actionListener);
		panelAccount.getButtonModify().addActionListener(actionListener);
		panelAccount.getButtonDelete().addActionListener(actionListener);
		
		setCurrentPanelFlags(true); //Change the current panel flag to true
	}
	
	public void showCreateModAccount(String buttonTitle) throws ComboModelException, CheckDataException, OperationImpossibleException, FormatScrollPaneException, LoadDriverException
	{
		int selectedRow = -1;
		boolean panelIsOk = false;
		
		setCurrentPanelFlags(false); //Change the current panel flag to false
					
		String[] tabHeader = {"Course name"}; //Create the headers of the columns
		GeneralMethod.clearAndSetHeader(tabHeader, "right"); //Fill the arrayList by the tabHeader

		GeneralInformation.createCourseFullModel(connection);
		GeneralMethod.setRightModel(GeneralInformation.getCourseLeftModel()); //Set the Generic Left JTable by the MyTableModel
		
		if(buttonTitle.equals("Create") == true) //If the button pushed is create
		{
			panelCreateModAccount = new PanelCreateModAccount(callDB, connection, 0); //Create a new CreateMod panel
			panelCreateModAccount.setName("panelCreateModAssessments"); //Set the name of the panel
			panelIsOk = true;
		}
		else
		{
			selectedRow = panelAccount.getTableLeft().getSelectedRow();
			if(buttonTitle.equals("Modify") == true && selectedRow != -1)
			{
				panelCreateModAccount = new PanelCreateModAccount(callDB, connection, (int)GeneralInformation.getMyAccountModel().getValueAt(selectedRow, 2)); //Create a new CreateMod panel
				panelCreateModAccount.setName("panelCreateModAssessments"); //Set the name of the panel
				panelIsOk = true;
			}
			else
				JOptionPane.showMessageDialog(null, "Please select a examination and try again !", "Empty selection", JOptionPane.INFORMATION_MESSAGE);
		}
		
		if(panelIsOk)
			addNewAndRemoveOldPanel(panelCreateModAccount); //Change the current panel
		
		setCurrentPanelFlags(true); //Change the current panel flag to true
	}
	
	public void showPanelStudent() throws CheckDataException, LoadDriverException, OpenConnectionException
	{
		setCurrentPanelFlags(false); //Change the current panel flag to false
		
		String[] tabHeader = {"Matricule","Firstname","Lastname"};
		GeneralMethod.clearAndSetHeader(tabHeader, "left");
		GeneralInformation.createStudentModel(connection);
		GeneralMethod.setLeftModel(GeneralInformation.getStudentModel());
		
		panelStudent = new PanelStudent(callDB, connection);
		panelStudent.setName("panelStudent");
		
		addNewAndRemoveOldPanel(panelStudent); //Change the current panel

		setCurrentPanelFlags(true); //Change the current panel flag to true
	}
	
	public void showPanelBackup() throws OpenConnectionException, LoadDriverException
	{
		setCurrentPanelFlags(false); //Change the current panel flag to false
		
		panelBackup = new PanelBackup(callDB, connection);
		panelBackup.setName("panelBackup");
		
		addNewAndRemoveOldPanel(panelBackup); //Change the current panel

		setCurrentPanelFlags(true); //Change the current panel flag to true
	}
	
	public void showPanelAboutManager(String titleLabel)
	{
		this.titleLabel = titleLabel;
		
		setCurrentPanelFlags(false); //Change the current panel flag to false
		
		panelAboutManager = new PanelAboutManager(this.titleLabel);
		panelAboutManager.setName("panelAboutManager");
		
		addNewAndRemoveOldPanel(panelAboutManager); //Change the current panel
		
		setCurrentPanelFlags(true); //Change the current panel flag to true
	}	

	private class MyActionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0)
		{
			try
			{
				if(arg0.getSource() == itemExit)
					System.exit(0);
				else if(arg0.getSource() == itemHome)
					showPanelMain();
				else if(arg0.getSource() == itemSwitch)
				{
					dispose();
					new WindowTeacher();
				}
				else if(arg0.getSource() == itemFAQ)
					showPanelAboutManager("F.A.Q");
				else if(arg0.getSource() == itemContact)
					showPanelAboutManager("Logs");
				else if(arg0.getSource() == itemHelp)
					showPanelAboutManager("Help");
				else if(arg0.getSource() == mainPanelManager.getButtonAccount() || arg0.getSource() == itemAccount)
					showPanelAccount();
				else if(arg0.getSource() == mainPanelManager.getButtonStudent() || arg0.getSource() == itemStudent)
					showPanelStudent();
				else if(arg0.getSource() == mainPanelManager.getButtonBackup() || arg0.getSource() == itemBackup)
					showPanelBackup();
				else if(panelAccountFlag == true)
					if(arg0.getSource() == panelAccount.getButtonCreate())
						showCreateModAccount("Create");
					else if(arg0.getSource() == panelAccount.getButtonModify()) // If at least a row is selected
						showCreateModAccount("Modify");
					else if(arg0.getSource() == panelAccount.getButtonDelete())
					{
						int selectedRow = panelAccount.getTableLeft().getSelectedRow();
						
						if(selectedRow != -1)
						{
							if(JOptionPane.showConfirmDialog(null, "Suppression of \""+ (String)GeneralInformation.getMyAccountModel().getValueAt(selectedRow, 0) + " " + (String)GeneralInformation.getMyAccountModel().getValueAt(selectedRow, 1) +"\" account.\nWould you like to continue ?", "Account suppression", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION);
							{
								new AccountMgmt(connection).deleteElement((int)GeneralInformation.getMyAccountModel().getValueAt(selectedRow, 2));
								JOptionPane.showMessageDialog(null, "Account is successfully deleted !", "Account suppression", JOptionPane.INFORMATION_MESSAGE);
							}
						}
						else
							JOptionPane.showMessageDialog(null, "An error occured during the suppression !", "Account suppression", JOptionPane.INFORMATION_MESSAGE);
					}
					else
						JOptionPane.showMessageDialog(null, "Please select a examination and try again !", "Empty selection", JOptionPane.INFORMATION_MESSAGE);
				else
					System.out.println("nothing selected"); //To avoid null pointer Exception
			}
			catch(HeadlessException | EraseDataException | InsertionDataException | CheckDataException | ComboModelException | OpenConnectionException | LoadDriverException | FormatScrollPaneException | OperationImpossibleException error)
			{
				TreatError.showError(error);
			}
			
			container.validate(); //Refresh the screen
		}
	}
	
	public void addMenuBar(MyActionListener actionListener)
	{
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		///////////////////////////////////////////////
		
		menuHome = new JMenu("Home");
		menuBar.add(menuHome);
		
		itemHome = new JMenuItem("Home");
		menuHome.add(itemHome);
		
		itemSwitch = new JMenuItem("Switch zone");
		menuHome.add(itemSwitch);
		
		itemExit = new JMenuItem("Exit");
		menuHome.add(itemExit);
		
		///////////////////////////////////////////////
		
		menuNavigation = new JMenu("Navigation");
		menuBar.add(menuNavigation);
		
		itemAccount = new JMenuItem("Account");
		menuNavigation.add(itemAccount);
		
		itemStudent = new JMenuItem("Student");
		menuNavigation.add(itemStudent);
		
		itemBackup = new JMenuItem("Backup");
		menuNavigation.add(itemBackup);
		
		///////////////////////////////////////////////
	
		menuAbout = new JMenu("Help");
		menuBar.add(menuAbout);
		
		itemFAQ = new JMenuItem("F.A.Q");
		menuAbout.add(itemFAQ);
		
		itemContact = new JMenuItem("Contact");
		menuAbout.add(itemContact);
		
		itemHelp = new JMenuItem("Help");
		menuAbout.add(itemHelp);
	}
	
	public void addItemMenuListener(ActionListener actionListener)
	{
		itemHome.addActionListener(actionListener);
		itemSwitch.addActionListener(actionListener);
		itemExit.addActionListener(actionListener);
		
		itemAccount.addActionListener(actionListener);
		itemStudent.addActionListener(actionListener);
		itemBackup.addActionListener(actionListener);
		
		itemFAQ.addActionListener(actionListener);
		itemContact.addActionListener(actionListener);
		itemHelp.addActionListener(actionListener);
	}
	
	public void addNewAndRemoveOldPanel(JPanel currentPanel) //Remove the currentPanel and set a new currentPanel
	{
		container.remove(this.currentPanel);
		this.currentPanel = currentPanel;
		container.add(this.currentPanel);
	}
	
	public void setCurrentPanelFlags(boolean state) //Set the current panel flag to true
	{
		if(this.currentPanel.getName().equals("mainPanelManager") == state)
			mainPanelManagerFlag = state;
		else if(this.currentPanel.getName().equals("panelAccount") == state)
			panelAccountFlag = state;
		else if(this.currentPanel.getName().equals("panelStudent") == state)
			panelStudentFlag = state;
		else if(this.currentPanel.getName().equals("panelAboutManager") == state)
			panelAboutManagerFlag = state;
	}

	private class MyWindowListener extends WindowAdapter{
		public void windowClosing(WindowEvent arg0){System.exit(0);}} //If user close the main window -> EXIT
}
